package hotel.quartz.oversea.controller;

import hotel.base.oversea.controller.BaseController;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.SysCommonBaseReqVO;
import hotel.quartz.oversea.mapper.SysCommonBaseMapper;
import hotel.quartz.oversea.service.SysCommonBaseService;
import hotel.quartz.oversea.vo.SysCommonBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author zhangjinzheng
 * @date 2019-11-22
 */
@Slf4j
@RestController
@RequestMapping("/sysCommonBase")
@Api(value = "SysCommonBaseController", tags = {"基础信息管理接口"})
public class SysCommonBaseController extends BaseController {

    @Autowired
    private SysCommonBaseService sysCommonBaseService;

    @Autowired
    private SysCommonBaseMapper sysCommonBaseMapper;


    @GetMapping("/getSysCommonBaseById/{id}")
    @ApiOperation(value = "根据基础信息id查询基础信息信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String")
    public Result<SysCommonBase> getSysCommonBaseById(@PathVariable("id") String id) {
        if (ObjectUtils.isEmpty(id)) {
            return new Result<>();
        } else {
            return new Result<SysCommonBase>(sysCommonBaseService.getSysCommonBaseById(id));
        }
    }

    /**
     * 分页查询列表数据，条件查询
     *
     * @return
     */
    @PostMapping("/list/{id}")
    @ApiOperation(value = "分页、条件查询基础信息列表信息")
    @ApiImplicitParam(name = "sysCommonBase", value = "查询条件", required = true, dataType = "SysCommonBase", paramType = "body")
    public Result<List<SysCommonBase>> list(@PathVariable("id") String id) {
        return new Result<List<SysCommonBase>>(sysCommonBaseService.list(id));
    }


    /**
     * 根据ID查询基础信息信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询详细基础信息信息", notes = "id存在且大于0")
    @ApiImplicitParam(name = "id", value = "基础信息编号", required = true, dataType = "String")
    public Result<SysCommonBase> findById(@PathVariable String id) {
        if (ObjectUtils.isEmpty(id)) {
            return new Result<>();
        } else {
            return new Result<>(sysCommonBaseService.selectByKey(id));
        }
    }

    /**
     * 添加基础信息信息
     *
     * @param sysCommonBase
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加基础信息")
    @ApiImplicitParam(name = "sysCommonBase", value = "基础信息实体信息", required = true, dataType = "SysCommonBase", paramType = "body")
    public Result add(@RequestBody SysCommonBase sysCommonBase) {
        sysCommonBaseService.create(sysCommonBase);
        return new Result();
    }

    /**
     * 删除基础信息信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除基础信息")
    @ApiImplicitParam(name = "id", value = "基础信息编号", required = true, dataType = "String")
    public Result delete(@PathVariable String id) {
        sysCommonBaseService.delete(id);
        return new Result();
    }

    /**
     * 更新基础信息信息
     *
     * @param sysCommonBase
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "更新基础信息")
    @ApiImplicitParam(name = "sysCommonBase", value = "基础信息实体信息", required = true, dataType = "SysCommonBase", paramType = "body")
    public Result edit(@RequestBody SysCommonBase sysCommonBase) {
        sysCommonBaseService.update(sysCommonBase);
        return new Result();
    }


    /**
     * <H1>获取系统公共参数列表</H1>
     *
     * @param sysCommonBaseReqVO 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/10/12 16:03
     */
    @PostMapping("/getList")
    public Result<Object> getList(@RequestBody SysCommonBaseReqVO sysCommonBaseReqVO) {
        List<SysCommonBase> list =  sysCommonBaseService.getList(sysCommonBaseReqVO);
        return new Result<>(list);
    }

    @PostMapping("/queryCommonBaseByGroupHead")
    public Result<Object> getList(@RequestBody Map map) {
        List<SysCommonBase> list =  sysCommonBaseService.queryCommonBaseByGroupHead(map);
        //List<SysCommonBase> sysCommonBaseList = GsonUtils.jsonToList(GsonUtils.GsonString(list),SysCommonBase.class);
        log.info("queryCommonBaseByGroupHead:"+ GsonUtils.GsonString(list));
        return new Result(GsonUtils.GsonString(list),"success");
    }

    /***
    * @Author: lmf
    * @Date: 2020/12/9
     * 添加基础配置
    */
    @PostMapping("/addCommonBase")
    public Result<Object> addCommonBase(@RequestBody SysCommonBase SysCommonBase) {
        return sysCommonBaseService.addCommonBase(SysCommonBase);
    }
    /***
     * @Author: lmf
     * @Date: 2020/12/9
     * 更新基础配置
     */
    @PostMapping("/updateCommonBase")
    public Result<Object> updateCommonBase(@RequestBody SysCommonBase SysCommonBase) {
        return sysCommonBaseService.updateCommonBase(SysCommonBase);
    }
    /***
     * @Author: lmf
     * @Date: 2020/12/9
     * 删除基础配置
     */
    @PostMapping("/delCommonBase")
    public Result<Object> delCommonBase(@RequestBody SysCommonBase SysCommonBase) {
        return sysCommonBaseService.delCommonBase(SysCommonBase);
    }

    /**
     * 查询详细基础信息信息返回value根据key
     *
     * @param id
     * @return
     */
/*
    @GetMapping("/value/{id}")
    @ApiOperation(value = "查询详细基础信息信息返回value根据key")
    @ApiImplicitParam(name = "id", value = "基础信息编号", required = true, dataType = "String")
    public Result<Object> getBaseValueByKey1(@PathVariable String id) {
        if (ObjectUtils.isEmpty(id)) {
            return new Result<>();
        } else {
            Example example = new Example(SysCommonBase.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",id);
            criteria.andEqualTo("opState",0);
            SysCommonBase sysCommonBase = sysCommonBaseMapper.selectOneByExample(example);
            if(null == sysCommonBase){
                return new Result<>();
            }
            return new Result<>(sysCommonBase.getOpValue(),"success");
        }
    }
*/

    @GetMapping("/getLikeId")
    @ApiOperation(value = "查询详细基础信息信息根据模糊查询id",notes = "必须经过oauth2授权")
    public Result<Object> getBaseValueByKey(@RequestParam(value = "id")String id){
        if (ObjectUtils.isEmpty(id)) {
            return new Result<>();
        } else {
            Example example = new Example(SysCommonBase.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",id);
            criteria.andEqualTo("opState",0);
            SysCommonBase sysCommonBase = sysCommonBaseMapper.selectOneByExample(example);
            if(null == sysCommonBase){
                return new Result<>();
            }
            return new Result<>(sysCommonBase.getOpValue(),"success");
        }

    }

}
